<?php
/**
 * WpCommandController.php
 * 
 * This file contains a controller class to execute a command specfied by name
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.controller
 */

require_once (dirname(__FILE__).'/IWpController.php');

/**
 * A controller class to execute a command specfied by name
 * resolve the requested class.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.controller
 */
class WpCommandController implements IWpController
{
	private $_doFwd = false;
	private $_fwdCmd = '';
	private $_fwdView = '';
	private $_o = null;
	
	/**
	 * @var The default command, if no command was provided
	 */
	public $DefaultCommand = 'Index';
	
	/**
	 * @var The name of the request parameter storing the command
	 */
	public $ParamNameCommand = 'cmd';
	
	/**
	 * @var The resolver object, used to resolve the command
	 */
	public $Resolver = null;
	
	/**
	 * @var The request object
	 */
	public $Request = null;
	
	/**
	 * @var The response object (is available after calling the process method)
	 */
	public $Response = null;
	
	/**
	 * @var The view object (is available after calling the process method)
	 */
	public $View = null;
	
	/**
	 * Reads the name of the command from request
	 * @return The command name
	 */
	private function _getCmdName()
	{
		$p = $this->Request->getParam($this->ParamNameCommand);
		if (is_null($p) || $p === '') {
			return $this->DefaultCommand;
		}
		
		return $p;
	}
	
	/**
   * The class constructor
   * @param object $resolver A resolver object to resolve the requested class object
   * @param object $request The request object
   */
	public function __construct($resolver, $request)
	{
		$this->Resolver = $resolver;
		$this->Request = $request;
	}
	
	/**
	 * Retrieves the command object
	 * @return An instance of the command class
	 */
	public function getCommand()
	{
		if (!is_null($this->_o)) {
			return $this->_o;
		}
		
		$this->_o = $this->Resolver->resolve($this->_getCmdName());
		return $this->_o;
	}
	
	/**
   * Starts processing the request
   * @param $view The view object to use.
   * @param $response The response object
   */
	public function process($view, $response)
	{
		$this->View = $view;
		$this->Response = $response;
		
		$this->View->Viewname = $this->_getCmdName(); //By default, viewname is equal to command name
		$o = $this->getCommand();
		if (!is_null($o)) {
			$o->execute($this, $this->Request, $this->View);
						
			//Handle forwarding
			while ($this->_doFwd === true) {
				$o = $this->Resolver->resolve($this->_fwdCmd);
				$this->View->Viewname = $this->_fwdView;
				$this->_doFwd = false;
				$this->_fwdCmd = '';
				$this->_fwdView = '';
				$o->execute($this, $this->Request, $this->View);
			}
		}
		
		$this->View->render($response);
	}
	
	/**
   * Mark the controller to forward to another command
   * @param string $command[optional] The command to forward to
   * @param string $viewname[optional] The view to forward to
   */
	public function forward($command='', $viewname='')
	{
		$this->_doFwd = true;
		$this->_fwdCmd = $command;
		$this->_fwdView = $viewname;
	}
}

/* End of file WpCommandController.php */
/* Location: ./wildpack/mvc/controller/WpCommandController.php */
 